args Controls outcomes lab keep opt text0 ipw CreativityMeasure MDV  dp a1 a2 a3 a4 a5 a6 a7 a8 

preserve

replace Creativity=creativityt_`CreativityMeasure'_7


* All estimations
foreach o of local outcomes {

	if `ipw'==1{
		ge io=`o'!=.
		qui logit io $HDC_0
		drop io
		predict p, pr
		su p
	}

	if `ipw'==0{
		ge p=1
	}

	qui rifhdreg `o' `Controls' [pweight=1/p], rif(mean) rob
	drop p

	est sto es_`o'
	mat b = e(b)
	mat b = b'
	svmat b
	qui su b1 if b1!=0
	mat define p= r(N)
	drop b1
	qui estadd scalar Par=p[1,1]
	local x ""
	qui estadd local x
}

* count number of variables
local count: word count `outcomes'
local count=`count'+1
local text   `text0' prefoot(\cline{2-`count'}  \vspace{-.3cm}\\ ) postfoot(\vspace{-.3cm} \\ \hline ) posthead( \cline{2-`count'})
local textE  `text0' prefoot(\cline{2-`count'}  \vspace{-.3cm}\\ ) postfoot(\vspace{-.4cm}  \\)   posthead( \cline{2-`count'})

* Stats
if `MDV'==1{
	local stR "stats(N rifmean,     fmt(%11.0gc %8.2f)          labels("Number of individuals" "Mean dep. var."))"
}


if `MDV'==0{
	local stR "stats(N, fmt(%11.0gc) labels("Number of individuals"))"
}


* Panel I
if "`opt'"=="start"{
	esttab es* using results/Tables/`lab'.tex, ///
		replace `text' se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
		keep(Creativity `keep') refcat(Creativity ""  " " "", nolabel) ///
		order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability"  "generalab_sd_11" "Cognitive ability") ///
		cells(b(star fmt(`dp')) se(par) ".") nogaps  ///
		mtitles("`a1'" "`a2'" "`a3'" "`a4'" "`a5'" "`a6'" "`a7'" "`a8'") frag `stR'
}

* Intermediate
if "`opt'"=="appendI"{
	esttab es* using results/Tables/`lab'.tex, ///
		append `text' se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
		keep(Creativity `keep') refcat(Creativity ""  " " "", nolabel) ///
		order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability"  "generalab_sd_11" "Cognitive ability") ///
		cells(b(star fmt(`dp')) se(par) ".") nogaps  ///
		mtitles("`a1'" "`a2'" "`a3'" "`a4'" "`a5'" "`a6'" "`a7'" "`a8'") frag `stR' 
}

* final
if "`opt'"=="appendL"{
	esttab es* using results/Tables/`lab'.tex, ///
		append `textE' se star(* 0.1 ** 0.05 *** 0.01) b(%9.3f) se(%9.3f) nonumbers collabels(none) ///
		keep(Creativity `keep') refcat(Creativity ""  " " "", nolabel) ///
		order(Creativity) varlabels(Creativity Creativity CogAbility "Cognitive ability"  "generalab_sd_11" "Cognitive ability") ///
		cells(b(star fmt(`dp')) se(par) ".") nogaps  ///
		mtitles("`a1'" "`a2'" "`a3'" "`a4'" "`a5'" "`a6'" "`a7'" "`a8'") frag `stR'
}


est drop _all
restore
